<?php
defined('BASEPATH') OR exit('No direct script access allowed');


use QCloud_WeApp_SDK\Mysql\Mysql as DB;


class UserToken extends CI_Controller
{
    public function index()
    {
        $code = $_GET['code'];
        requestOpenid();

        if (!empty($code)) {
            $this->json([
                'code' => 0,
                'data' => $code
            ]);
        } else {
            $this->json([
                'code' => -1,
                'error' => $code['error']
            ]);
        }
    }
    public function requst(){
        request.get({
    uri: 'https://api.weixin.qq.com/sns/jscode2session',
    json: true,
    qs: {
            grant_type: 'authorization_code',
      appid: '你小程序的APPID',
      secret: '你小程序的SECRET',
      js_code: code
    }
  }, (err, response, data) => {
            if (response.statusCode === 200) {
                console.log("[openid]", data.openid)
      console.log("[session_key]", data.session_key)

      //TODO: 生成一个唯一字符串sessionid作为键，将openid和session_key作为值，存入redis，超时时间设置为2小时
      //伪代码: redisStore.set(sessionid, openid + session_key, 7200)

      res.json({ sessionid: sessionid })
    } else {
                console.log("[error]", err)
      res.json(err)
    }
        })

    }
}
